
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第九章 结构体类型(2) · C语言程序设计精髓程</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="马小安">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-plus/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-disqus/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-prism/prism-base16-ateliersulphurpool.light.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-terminal/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="chapter9_3.html" />
    
    
    <link rel="prev" href="chapter9_1.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                        <b>1.1.</b>
                    
                    笔记内容介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1.html">
            
                <a href="chapter1.html">
            
                    
                        <b>1.2.</b>
                    
                    第一章 基础知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter2.html">
            
                <a href="chapter2.html">
            
                    
                        <b>1.3.</b>
                    
                    第二章 数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter3.html">
            
                <a href="chapter3.html">
            
                    
                        <b>1.4.</b>
                    
                    第三章 顺序结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter4.html">
            
                <a href="chapter4.html">
            
                    
                        <b>1.5.</b>
                    
                    第四章 选择结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter5.html">
            
                <a href="chapter5.html">
            
                    
                        <b>1.6.</b>
                    
                    第五章 循环结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="chapter6.html">
            
                <a href="chapter6.html">
            
                    
                        <b>1.7.</b>
                    
                    第六章 数组概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="chapter7.html">
            
                <a href="chapter7.html">
            
                    
                        <b>1.8.</b>
                    
                    第七章 函数与预编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="chapter8_1.html">
            
                <a href="chapter8_1.html">
            
                    
                        <b>1.9.</b>
                    
                    第八章 指针类型(1)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="chapter8_2.html">
            
                <a href="chapter8_2.html">
            
                    
                        <b>1.10.</b>
                    
                    第八章 指针类型(2)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="chapter9_1.html">
            
                <a href="chapter9_1.html">
            
                    
                        <b>1.11.</b>
                    
                    第九章 结构体类型(1)
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.12" data-path="chapter9_2.html">
            
                <a href="chapter9_2.html">
            
                    
                        <b>1.12.</b>
                    
                    第九章 结构体类型(2)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="chapter9_3.html">
            
                <a href="chapter9_3.html">
            
                    
                        <b>1.13.</b>
                    
                    第九章 结构体类型(3)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="chapter10.html">
            
                <a href="chapter10.html">
            
                    
                        <b>1.14.</b>
                    
                    第十章 位运算
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="chapter11.html">
            
                <a href="chapter11.html">
            
                    
                        <b>1.15.</b>
                    
                    第十一章 排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Glossary.html">
            
                <a href="Glossary.html">
            
                    
                        <b>1.16.</b>
                    
                    其他资源
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >第九章 结构体类型(2)</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;">&#x7ED3;&#x6784;&#x4F53;&#x7C7B;&#x578B;</h1>
<pre class="language-"><code>#include &lt;stdio.h&gt;
#define N 40

int main()
{
    int sum[10];
    typedef struct date
    {
        int year;
        int month;
        int day;
    }DATE;
    typedef struct course
    {
        int math;
        int computer;
        int english;
        int chinese;
    }COURSE;

    typedef struct student
    {
        int stuentID;
        int age;
        DATE birthday;
        char name[20];
        COURSE score;
    }STUDENT;

    STUDENT stu[30] = { 
                        { 101,22,{1990,11,31},&quot;ma1&quot;,{60,79,90,88}},
                        { 102,22,{1991,10,31},&quot;ma2&quot;,{60,79,70,88}},
                        { 103,23,{1992,12,31},&quot;ma3&quot;,{60,79,50,88}},
                        { 104,24,{1993,11,31},&quot;ma4&quot;,{60,79,30,88}}                        
                        };

    for (int i = 0; i &lt; 4; i++)
    {
        sum[i] = 0;
        for (int j = 0; j &lt; 4; j++)
        {
            sum[i] = stu[i].score.math + stu[i].score.computer + stu[i].score.english +
                stu[i].score.chinese;
        }
        printf(&quot;%4d%6d%8d-%d-%d%4s%8.1lf\n&quot;,
            stu[i].stuentID,
            stu[i].age,
            stu[i].birthday.year,
            stu[i].birthday.month,
            stu[i].birthday.day,
            stu[i].name,
            sum[i]/4.0
            );
    }



    getchar();
    getchar();
    return 0;
}
</code></pre><h1 id="&#x7ED3;&#x6784;&#x4F53;&#xFF08;&#x6307;&#x9488;&#xFF09;&#x505A;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x7684;&#x53C2;&#x6570;">&#x7ED3;&#x6784;&#x4F53;&#xFF08;&#x6307;&#x9488;&#xFF09;&#x505A;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x7684;&#x53C2;&#x6570;</h1>
<pre class="language-"><code>#include &lt;stdio.h&gt; 
typedef    struct point
{
    int x;
    int y;
    int z;
} POINT;
void Func1(POINT p);
void Func2(POINT *p);
int main()
{
    POINT position1 = { 0,0,0 },*position2;

    printf(&quot;B:\n&quot;);
    printf(&quot;%d,%d,%d\n&quot;,position1.x,position1.y,position1.z);
    Func1(position1);//&#x7ED3;&#x6784;&#x4F53;&#x53D8;&#x91CF;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x53C2;&#x6570;
    printf(&quot;A Func1:\n&quot;);
    printf(&quot;%d,%d,%d\n&quot;, position1.x, position1.y, position1.z);
    printf(&quot;B:\n&quot;);
    position2 = &amp;position1;
    printf(&quot;%d,%d,%d\n&quot;, position2-&gt;x, position2-&gt;y, (*position2).z);
    printf(&quot;A Func2:\n&quot;);
    Func2(position2);//&#x7ED3;&#x6784;&#x4F53;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x53C2;&#x6570;
    printf(&quot;%d,%d,%d\n&quot;, position2-&gt;x, position2-&gt;y, (*position2).z);

    getchar();
    getchar();
    return 0;
}
void Func1(POINT p)
{
    p.x = 1;
    p.y = 1;
    p.z = 1;
    //return p;//&#x7ED3;&#x6784;&#x4F53;&#x53D8;&#x91CF;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;,&#x8FD4;&#x56DE;&#x591A;&#x4E2A;&#x6570;&#x503C;&#xFF0C;&#x4F46;&#x6548;&#x7387;&#x4F4E;
}
void Func2(POINT *p)
{
    p-&gt;x = 2;
    p-&gt;y = 2;
    p-&gt;z = 2;
}
</code></pre><h1 id="&#x7ED3;&#x6784;&#x5C01;&#x88C5;&#x51FD;&#x6570;&#x53C2;&#x6570;">&#x7ED3;&#x6784;&#x5C01;&#x88C5;&#x51FD;&#x6570;&#x53C2;&#x6570;</h1>
<pre class="language-"><code>#include &lt;stdio.h&gt;
#define N 40
typedef struct date
{
    int year;
    int month;
    int day;
}DATE;
typedef struct student
{
    int stuentID;
    int studentage;
    DATE birthday;
    char studentname[20];
    int score[4];
    int aver;
}STUDENT;

int main()
{


    getchar();
    getchar();
    return 0;
}
void average(STUDENT stu[], int n, int m)
{
    int i, j, sum[N];
    for (i = 0; i &lt; n; i++)
    {
        sum[i] = 0;
        for (j = 0; j &lt; m; j++)
        {
            sum[i] = sum[i] + stu[i].score[j];
        }
        stu[i].aver = (float)sum[i] / m;
    }

}
</code></pre><h1 id="&#x6279;&#x91CF;&#x521B;&#x5EFA;&#x7ED3;&#x70B9;">&#x6279;&#x91CF;&#x521B;&#x5EFA;&#x7ED3;&#x70B9;</h1>
<pre class="language-"><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#define N 40

typedef struct student
{
    int studentID;
    char studentName[20];
    char studentSex;
    struct student *next;
}STUDENT;

STUDENT * Build(int num);
int main()
{
    STUDENT *head;
    STUDENT *p;
    int j = 1;
    printf(&quot;pls input ID,name,Sex\n&quot;);
    head = Build(3);
    p = head;
    while (p != NULL)
    {
        printf(&quot;%d%8s\n&quot;, j, p-&gt;studentName);
        p=p-&gt;next;
        j++;
    }

    getchar();
    getchar();
    return 0;
}
STUDENT * Build(int num)
{
    STUDENT *Pt, *prePt, *head;

    //&#x7533;&#x8BF7;&#x5934;&#x7ED3;&#x70B9;
    Pt = (STUDENT *)malloc(sizeof(STUDENT));

    if (Pt != NULL)
    {
        scanf(&quot;%d%s%c&quot;, &amp;Pt-&gt;studentID, Pt-&gt;studentName, &amp;Pt-&gt;studentSex);
        getchar();//&#x8BFB;&#x8D70;&#x7F13;&#x5B58;&#x533A;&#x7684;&#x56DE;&#x8F66;
        head = Pt;
        prePt = Pt;//&#x4F7F;&#x7528;prePt&#x6765;&#x63A7;&#x5236;&#x5C3E;&#x7ED3;&#x70B9;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x901A;&#x8FC7;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x7ED3;&#x70B9;&#x7684;&#x6307;&#x9488;&#x53D8;&#x91CF;&#x6765;&#x64CD;&#x4F5C;
    }
    else
    {
        printf(&quot;Failed\n&quot;);
        exit(0);
    }

    //&#x6279;&#x91CF;&#x521B;&#x5EFA;&#x4E2D;&#x95F4;&#x8282;&#x70B9;
    for (int i = 0; i &lt;num; ++i)
    {
        Pt = (struct student *)malloc(sizeof(struct student));
        if(Pt != NULL)
        {
            printf(&quot;pls input [%d]ID,name,Sex\n&quot;, i);
            scanf(&quot;%d%s%c&quot;, &amp;Pt-&gt;studentID, Pt-&gt;studentName, &amp;Pt-&gt;studentSex);
            getchar();//&#x8BFB;&#x8D70;&#x7F13;&#x5B58;&#x533A;&#x7684;&#x56DE;&#x8F66;
            prePt-&gt;next = Pt;//&#x65B0;&#x8282;&#x70B9;&#x548C;&#x4E4B;&#x524D;&#x7684;&#x8282;&#x70B9;&#x94FE;&#x63A5;&#x8D77;&#x6765;
            prePt = Pt;//prePt&#x603B;&#x662F;&#x6307;&#x5411;&#x94FE;&#x8868;&#x7684;&#x5C3E;&#x8282;&#x70B9;
        }
        else
        {
            printf(&quot;Failed\n&quot;);
            exit(0);
        }

    }
    //&#x521B;&#x5EFA;&#x5C3E;&#x8282;&#x70B9;
    prePt-&gt;next = NULL;
    return head;
}
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="chapter9_1.html" class="navigation navigation-prev " aria-label="Previous page: 第九章 结构体类型(1)">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="chapter9_3.html" class="navigation navigation-next " aria-label="Next page: 第九章 结构体类型(3)">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第九章 结构体类型(2)","level":"1.12","depth":1,"next":{"title":"第九章 结构体类型(3)","level":"1.13","depth":1,"path":"chapter9_3.md","ref":"chapter9_3.md","articles":[]},"previous":{"title":"第九章 结构体类型(1)","level":"1.11","depth":1,"path":"chapter9_1.md","ref":"chapter9_1.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","highlight","-livereload","search-plus@^0.0.11","disqus@^0.1.0","prism@^2.1.0","prism-themes@^0.0.2","emphasize@^1.1.0","katex@^1.1.3","splitter@^0.0.8","terminal@^0.3.2","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"prism":{"css":["prism-themes/themes/prism-base16-ateliersulphurpool.light.css"]},"disqus":{"useIdentifier":false,"shortName":"gitbookuse"},"emphasize":{},"splitter":{},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"prism-themes":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"terminal":{"copyButtons":true,"fade":false,"style":"flat"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":true},"search-plus":{}},"theme":"default","author":"马小安","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"C语言程序设计精髓程","language":"zh-hans","gitbook":"*","description":"苏小红(哈工大)"},"file":{"path":"chapter9_2.md","mtime":"2018-01-31T01:17:29.881Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-01-31T05:09:23.850Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search-plus/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-plus/search.js"></script>
        
    
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/URI.js/1.16.1/URI.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-disqus/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-terminal/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

